package com.shgsh.platform.controller;

import com.shgsh.attribute.model.WeatherData;
import com.shgsh.platform.service.WeatherDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;

/**
 * Created by Evelyn on 2015/8/8.
 */
@Controller
@RequestMapping("/platform/weather")
public class WeatherDataController {
    @Autowired
    WeatherDataService weatherDataService;

    @RequestMapping(value = "edit", method = RequestMethod.GET)
    public String get(String id, Model model) {
        if (id != null) {
            WeatherData weatherData = weatherDataService.get(id);
            model.addAttribute("weatherData", weatherData);
        }
        return "platform/weather/edit";
    }

    @RequestMapping(value = "edit", method = RequestMethod.POST)
    public String edit(WeatherData weatherData) {
        weatherDataService.add(weatherData);
        return "redirect:list";
    }

    @RequestMapping("list")
    public String list(Model model) {
        ArrayList weatherDatas = weatherDataService.getAll();
        model.addAttribute("weatherDatas", weatherDatas);
        return "platform/weather/list";
    }

    @RequestMapping("delete")
    public String delete(String id) {
        if (!id.isEmpty()) {
            weatherDataService.delete(id);
        }
        return "redirect:list";
    }
}
